'use strict';

const Controller = require('egg').Controller;
const api = require('../../config/api');

class EventsController extends Controller {
  async index() {
    const { ctx } = this;
    // 处理请求参数：
    const page = ctx.request.query.page;
    const id = ctx.request.query.id;
    if (id) {
      // 新闻详情
      const res = await ctx.curl(`${api.basicUrl}/Event/getEventItem`, {
        method: 'GET',
        contentType: 'json',
        dataType: 'json',
        data: {
          eventid: id,
        },
      });
      ctx.logger.info(`page: ${page}, res: ${res.data.rows}`);
      await ctx.render('events.html', { pageData: res.data, isDetail: true });
    } else {
      // 获取新闻
      const res = await ctx.curl(`${api.basicUrl}/Event/getEventList`, {
        method: 'GET',
        contentType: 'json',
        dataType: 'json',
        data: {
          page: page || 1,
          pagesize: 9,
        },
      });
      ctx.logger.info(`page: ${page}, res: ${res.data.rows}`);
      console.log('EventsController:', res.data);
      await ctx.render('events.html', { pageData: res.data, isDetail: false });
    }
  }
}

module.exports = EventsController;
